﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CrankShaftAnalysisSystem.Devices;
using CrankShaftAnalysisSystem.Controller;

namespace CrankShaftAnalysisSystem
{
    public partial class FormPLC : Form
    {
        public PLCMaster PLC { get; private set; }
        public FormPLC()
        {
            InitializeComponent();
            PLC = PLCMaster.Instance;
        }

        private void FormPLC_Load(object sender, EventArgs e)
        {
            tmLoop.Start();
        }

        private void FormPLC_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            PLC.SetSpeed(1, (int)nmVel1.Value);
            PLC.SetSpeed(2, (int)nmVel2.Value);
            PLC.SetSpeed(3, (int)nmVel3.Value);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            PLC.SetSpeed(1, 0);
            PLC.SetSpeed(2, 0);
            PLC.SetSpeed(3, 0);
            nmVel1.Value = 0;
            nmVel2.Value = 0;
            nmVel3.Value = 0;
        }

        private void tmLoop_Tick(object sender, EventArgs e)
        {
            lbState.BackColor = PLC.Device.Connected ? Color.Green : Color.Red;
            lbEncoderState.Text = $"State: {EncoderERM2400.Instance.Device.State}";
            lbAngleControllerState.Text = $"State: {AngleController.State}";
            lbLinearControllerState.Text = $"State: {LinearController.State}";
            lbPressControllerState.Text = $"State: {PressureController.State}";
            lbAxis3Height.Text = $"{PressureController.Height:F3}mm";
        }

        private void btnEncoderFindRef_Click(object sender, EventArgs e)
        {
            if (EncoderERM2400.Instance.Device.State == MeasureSystem.State.BUSY)
                return;
            EncoderERM2400.Instance.Device.ResetRef();
            EncoderERM2400.Instance.Device.WaitRef();
        }

        private void btnSetAngPos_Click(object sender, EventArgs e)
        {
            try // SetPosition的异常是在新的Task里面的，所以这个try应该是捕捉不到的
            {
                if (AngleController.State == MeasureSystem.State.BUSY)
                    return;
                AngleController.SetPosition(0);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void btnSetHeight_Click(object sender, EventArgs e)
        {
            if (LinearController.State == MeasureSystem.State.BUSY)
                return;
            LinearController.SetPosition((double)nmHeight.Value);
        }

        private void btnMt2571Out_Click(object sender, EventArgs e)
        {
            LogicalController.MT2571Out();
        }

        private void btnMt2571Back_Click(object sender, EventArgs e)
        {
            LogicalController.MT2571Back();
        }

        private void btnVacuumOff_Click(object sender, EventArgs e)
        {
            LogicalController.VacuumOff();
        }

        private void btnVacuumOn_Click(object sender, EventArgs e)
        {
            LogicalController.VacuumOn();
        }

        private void btnPressPart_Click(object sender, EventArgs e)
        {
            PressureController.PressPart();
        }

        private void btnLoosePart_Click(object sender, EventArgs e)
        {
            PressureController.MoveUp();
        }
    }
}
